//
//  ActiveCircleView.m
//  iMoc
//
//  Created by 微我网络 on 15/1/12.
//  Copyright (c) 2015年 lamcelot. All rights reserved.
//

#import "ActiveCircleView.h"
#import <QuartzCore/QuartzCore.h>
#import "PNChart.h"

@interface ActiveCircleView ()

@property (nonatomic) CGFloat externalRadius;
@property (nonatomic) CGFloat internalRadius;
@property (nonatomic) CGFloat ringWidth;

@property (nonatomic,strong) NSMutableArray *arcLayers;
//@property (nonatomic,strong) UIVisualEffectView *visualView;
@property (nonatomic,strong) PNCircleChart *circleChart;

@end

@implementation ActiveCircleView

- (instancetype)initWithCenter:(CGPoint)point width:(CGFloat)width style:(ActiveCircleStyle)style
{
    self = [super initWithFrame:CGRectMake(point.x-width/2, point.y-width/2, width, width)];
    if (self) {
        
//        _externalRadius = width/2;
//        _ringWidth = ringWidth;
//        _internalRadius = _externalRadius-ringWidth;
        self.layer.cornerRadius = _externalRadius;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width)];
        _contentView.layer.cornerRadius = _internalRadius;
        _contentView.layer.masksToBounds = YES;
        [self addSubview:_contentView];
        
//        if (style == ActiveCircleStyleBlur) {
//            
//            _visualView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
//            _visualView.frame = CGRectMake(0, 0, width, width);
////            _visualView.clipsToBounds = YES;
//            [self.contentView addSubview:_visualView];
//        }
        
//        _arcLayer = [CAShapeLayer layer];
//        _arcLayer.fillColor = [UIColor clearColor].CGColor;
//        _arcLayer.strokeColor = [UIColor blackColor].CGColor;
//        _arcLayer.lineWidth = ringWidth;
//        [self.layer addSublayer:_arcLayer];
        
//        _circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(-ringWidth/2, -ringWidth/2, width+ringWidth*2, width+2*ringWidth)
//                                                      total:@360
//                                                    current:@0
//                                                  clockwise:YES
//                                                     shadow:YES];
        if (style == 0) {
            _circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 0, width, width) total:@360 current:@0 clockwise:YES shadow:YES shadowColor:[UIColor lightGrayColor] displayCountingLabel:NO overrideLineWidth:@2];
            _circleChart.backgroundColor = [UIColor clearColor];
            _circleChart.strokeColor     = [UIColor colorWithRed:17/255.f green:121/255.f blue:176/255.f alpha:1];
            _circleChart.chartType       = PNChartFormatTypeNone;
            _circleChart.countingLabel.hidden = YES;
            _circleChart.strokeColorGradientStart = [UIColor colorWithRed:17/255.f green:121/255.f blue:176/255.f alpha:1];
            _circleChart.circleBackground.strokeColor = [UIColor lightGrayColor].CGColor;
            [self addSubview:_circleChart];
            
            [_circleChart strokeChart];
        }
        else if (style == 1) {

            CAShapeLayer *shadowLayer = [CAShapeLayer layer];
            shadowLayer.fillColor = [UIColor clearColor].CGColor;
            shadowLayer.strokeColor = [UIColor lightGrayColor].CGColor;
            shadowLayer.lineWidth = 1;
            UIBezierPath *bezier = [UIBezierPath bezierPath];
            [bezier addArcWithCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:128.5 startAngle:-M_PI_2 endAngle:1.5*M_PI clockwise:YES];
            shadowLayer.path = bezier.CGPath;
            [self.layer addSublayer:shadowLayer];

            _arcLayers = [NSMutableArray array];
        }
    }
    return self;
}

- (void)strokePieWithValue0:(CGFloat)value0 value1:(CGFloat)value1 value2:(CGFloat)value2 {
    
    for (CAShapeLayer *layer in _arcLayers) {
        
        [layer removeFromSuperlayer];
    }
    [_arcLayers removeAllObjects];
    
//    if (value0 == 0) {
//        value0 = 0.1;
//    }
//    if (value1 == 0) {
//        value1 = 0.1;
//    }
//    if (value2 == 0) {
//        value2 = 0.1;
//    }
    
    CGFloat total = value0+value1+value2;
    NSArray *array = @[@(value0),@(value1),@(value2)];
    NSArray *color = @[[UIColor colorWithRed:156/255.f green:67/255.f blue:210/255.f alpha:1],
                       [UIColor colorWithRed:140/255.f green:108/255.f blue:159/255.f alpha:1],
                       [UIColor colorWithRed:255/255.f green:229/255.f blue:174/255.f alpha:1]];
    for (int i = 0; i < 3; i++) {
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.fillColor   = [UIColor clearColor].CGColor;
        layer.strokeColor = [(UIColor *)color[i] CGColor];
        layer.lineWidth   = 2;
        
        UIBezierPath *bezier = [UIBezierPath bezierPath];
        CGFloat start = 0.0,end = 0.0;
        switch (i) {
            case 0:
                start = -M_PI_2;
                end = [array[0] floatValue]/total*2*M_PI-M_PI_2;
                break;
                
            case 1:
                start = [array[0] floatValue]/total*2*M_PI-M_PI_2;
                end = ([array[0] floatValue]+[array[1] floatValue])/total*2*M_PI-M_PI_2;
                break;
                
            case 2:
                start = ([array[0] floatValue]+[array[1] floatValue])/total*2*M_PI-M_PI_2;
                end = 2*M_PI-M_PI_2;
                break;
                
            default:
                break;
        }
        [bezier addArcWithCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:128 startAngle:start endAngle:end clockwise:YES];
        layer.path = bezier.CGPath;
        [self.layer addSublayer:layer];
        [_arcLayers addObject:layer];
    }
}

- (void)startAnimationWithAngle:(CGFloat)angle {
    
//    NSInteger value = arc4random()%360;
//    CGFloat realAngle = angle*M_PI/180;
//    UIBezierPath *bezier = [UIBezierPath bezierPath];
//    [bezier addArcWithCenter:CGPointMake(_externalRadius, _externalRadius)
//                      radius:_externalRadius-_ringWidth/2
//                  startAngle:-M_PI_2
//                    endAngle:realAngle-M_PI_2
//                   clockwise:YES];
//    _arcLayer.path = bezier.CGPath;
//
//    float duration = realAngle/M_PI/2;
//    
//    CABasicAnimation *bas = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//    bas.duration = duration;
//    bas.fromValue = [NSNumber numberWithInteger:0];
//    bas.toValue   = [NSNumber numberWithInteger:1];
//    [_arcLayer addAnimation:bas forKey:@"key"];
    
//    POPDecayAnimation *pop = [POPDecayAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
//    pop.fromValue = [NSNumber numberWithInt:20];
//    pop.toValue = [NSNumber numberWithInt:0];
//    [_arcLayer pop_addAnimation:pop forKey:@"stroke"];
    [_circleChart updateChartByCurrent:@(0)];
    [_circleChart setDuration:2.0];
    [_circleChart updateChartByCurrent:@(angle)];
//    [_circleChart setCurrent:@(angle)];
//    [_circleChart strokeChart];
}

- (void)setRingColor:(UIColor *)ringColor {
    
    _circleChart.strokeColor = ringColor;
}

- (void)setRingBackGroundColor:(UIColor *)ringBackGroundColor {
    
    _circleChart.circleBackground.strokeColor = ringBackGroundColor.CGColor;
}

@end
